package com.bjtech.web.action.management;

import java.util.Date;
import java.util.List;

import org.apache.struts2.ServletActionContext;

import com.bjtech.dao.UserConsultInfoDao;
import com.bjtech.dao.UserConsultReplyDao;
import com.bjtech.domain.LoginUserInfo;
import com.bjtech.domain.User;
import com.bjtech.domain.UserConsultInfo;
import com.bjtech.domain.UserConsultReply;
import com.bjtech.domain.query.UserConsultInfoQuerySpec;
import com.bjtech.web.action.LyheatBaseAction;
import com.bjtech.web.util.Constants;

public class UserConsultInfoAction extends LyheatBaseAction {

	private static final long serialVersionUID = 1L;
	private UserConsultInfoDao userConsultInfoDao;
	private UserConsultInfoQuerySpec selectBean;
	
	private String custom;
	private UserConsultReplyDao userConsultReplyDao;
	private List<UserConsultReply> replyList;
	
	public void prepare() throws Exception {

	}
	
	public UserConsultInfoAction() throws Exception{
		model = new UserConsultInfo();
		selectBean = new UserConsultInfoQuerySpec();
	}
	
	public void setUserConsultInfoDao(UserConsultInfoDao userConsultInfoDao) {
		this.userConsultInfoDao = userConsultInfoDao;
	}

	public String execute(){
		return this.list();
	}
	
	public String list(){
		User user = this.getUser();
		if(user.getUserRole()!=null&&user.getUserId()!=null){
			if(user.getUserRole().intValue()==7){
					selectBean.setUser_id(user.getUserId());
			}
		}
		selectBean.queryParamString();
		list = this.userConsultInfoDao.selectEntityPaginatedList(new UserConsultInfo(),selectBean);
		return "list";
	}
	
	/**
	 * 管理员回复
	 * @return
	 */
	public String reply(){
		selectBean.queryParamString();
		list = this.userConsultInfoDao.selectEntityPaginatedList(new UserConsultInfo(),selectBean);
		custom="UserConsultReply/list.jsp";
		return "custom";
	}
	public String add(){
		return "edit";
	}
	
	public String save(){
		UserConsultInfo entity = (UserConsultInfo)model;
		if(entity.getId()==null){
			entity.setConsult_date(new Date());
			User current_user = (User) ServletActionContext.getRequest().getSession().getAttribute(Constants.MANAGEMENT);
			entity.setUser_id(current_user.getUserId());
			userConsultInfoDao.insertEntity(entity);
		}else{
			userConsultInfoDao.updateEntity(entity);
		}
		return "success";
	}
	
	public String edit(){
		model = this.userConsultInfoDao.selectEntity(new UserConsultInfo(),id);
		return "edit";
	}

	public String view(){
		model = this.userConsultInfoDao.selectEntity(new UserConsultInfo(),id);
		replyList = this.userConsultReplyDao.selectRelyListByInfoId(id);
		return "view";
	}
	
	public String delete(){
		this.userConsultReplyDao.deleteUserConsultReplyByInfoId(id);
		model = this.userConsultInfoDao.deleteEntity(new UserConsultInfo(),id);
		return "success";
	}
	
	public UserConsultInfoQuerySpec getSelectBean() {
		return selectBean;
	}

	public void setSelectBean(UserConsultInfoQuerySpec selectBean) {
		this.selectBean = selectBean;
	}

	public String getCustom() {
		return custom;
	}

	public void setCustom(String custom) {
		this.custom = custom;
	}


	public List<UserConsultReply> getReplyList() {
		return replyList;
	}

	public void setReplyList(List<UserConsultReply> replyList) {
		this.replyList = replyList;
	}

	public UserConsultInfoDao getUserConsultInfoDao() {
		return userConsultInfoDao;
	}

	public UserConsultReplyDao getUserConsultReplyDao() {
		return userConsultReplyDao;
	}

	public void setUserConsultReplyDao(UserConsultReplyDao userConsultReplyDao) {
		this.userConsultReplyDao = userConsultReplyDao;
	}
}
